/*
 * Created on June 29, 2007
 * 
 * Copyright (c) Patrick Armstrong 2002-2007. All Rights Reserved
 */
package org.dime.mine.test;

import java.security.InvalidParameterException;

import org.dime.mine.game.BasicBoard;
import org.dime.mine.game.Board;

/**
 * ConfigurableBoard // TODO Add type summary
 * 
 * @author Owner
 */
public class ConfigurableBoard extends BasicBoard
{
    public ConfigurableBoard(int[][] boardLayout)
    {
        // construct a simple base object because we will just be overwriting
        // it anyway
        super(1, 1, 0);

        if (!validateBoardLayout(boardLayout))
            throw new InvalidParameterException(
                    "boardLayout must have at least 1 row and at least 1 column and consist of only 0's and 9's.");

        initialize(generateIndicators(boardLayout), countMines(boardLayout));
    }

    private boolean validateBoardLayout(int[][] boardLayout)
    {
        if (boardLayout.length <= 0 && boardLayout[0].length <= 0)
            return false;

        for (int i = 0; i < boardLayout.length; i++)
        {
            for (int j = 0; j < boardLayout[i].length; j++)
            {
                if (!(boardLayout[i][j] == Board.MINE || boardLayout[i][j] == 0))
                    return false;
            }
        }

        return true;
    }

    private int countMines(int[][] boardLayout)
    {
        int count = 0;
        for (int i = 0; i < boardLayout.length; i++)
        {
            for (int j = 0; j < boardLayout[i].length; j++)
            {
                if (boardLayout[i][j] == Board.MINE)
                    count++;
            }
        }
        return count;
    }
}
